// var n;
// var r = cc.Vec2;
// var c = cc.BlockInputEvents;
// var l = cc.Layout.ResizeMode;
// var d = cc.Layout.AxisDirection;
// var u = cc.Layout.VerticalDirection;
// var h = cc.Layout.HorizontalDirection;
// var p = cc.Sprite.Type;
// var g = cc.Sprite.SizeMode;
// var $chestDialog = require("./AL_Framework_Chest_Dialog");
// var $buttonAction = require("./AL_Framework_Button_Action");
// var y = cc._decorator;
// var _ = y.ccclass;
// var v = y.property;
// var w = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.node_game_layout = null;
//         t.resumeButton = null;
//         t.button_start_position = new cc.Vec3(0, -540, 0);
//         t.button_end_position = new cc.Vec3(0, -360, 0);
//         t.buttonClickTimes = 2;
//         t.handle_button_callback = null;
//         t.xiaohui_huidiao = null;
//         t.limitY = 0;
//         t.click_time = 0;
//         t.is_banner_show = !1;
//         t.isStartShow = !1;
//         t.loadFailTimes = 0;
//         return t;
//     }
//     var o;
//     __extends(t, e);
//     o = t;
//     t.prototype.onLoad = function () {
//         ailin_sdk_manager.yincangBannerGuanggao();
//     };
//     t.prototype.start = function () {
//         var e = this;
//         this.resumeButton.position = this.button_start_position;
//         this.resumeButton.on(cc.Node.EventType.TOUCH_END, this.handle_event_click_button_resume, this);
//         this.tianchongYouxiBuju();
//         var t = cc.repeatForever(
//             cc.sequence(
//                 cc.moveBy(1, new r(0, 100)),
//                 cc.callFunc(function () {
//                     if (e.node_game_layout.position.y > e.limitY) {
//                         console.log("move down");
//                         e.node_game_layout.stopAllActions();
//                         e.node_game_layout.runAction(o);
//                     }
//                 })
//             )
//         );
//         var o = cc.repeatForever(
//             cc.sequence(
//                 cc.moveBy(1, new r(0, -100)),
//                 cc.callFunc(function () {
//                     if (e.node_game_layout.position.y < 600) {
//                         e.node_game_layout.stopAllActions();
//                         e.node_game_layout.runAction(t);
//                     }
//                 })
//             )
//         );
//         this.node_game_layout.runAction(t);
//         var i = ailin_sdk_manager.instance.qg_gengduo_youxi_shuju[0];
//         if (i) {
//             ailin_sdk_manager.tiaozhuanDaoXinApp({
//                 source: oppo_get_online_dataId,
//                 data: i.jumpData,
//                 success: function () {
//                     try {
//                         ailin_sdk_manager.shezhiGuanggaoZhuizong(
//                             ailin_sdk_manager.instance.qg_gengduo_youxi_shuju[0].id,
//                             8,
//                             ailin_sdk_manager.instance.qg_gengduo_youxi_shuju[0].targetUniqueGame
//                         );
//                     } catch (e) {
//                         console.log("tiaozhuanDaoXinApp: " + JSON.stringify(e));
//                     }
//                 }
//             });
//         }
//     };
//     t.prototype.handle_event_click_button_resume = function () {
//         console.log("chestDialog handle_event_click_button: 是否走流程 " + ailin_sdk_manager.instance.dangqian_duihuakuang_shifou_shiyongzhong);
//         if (!ailin_sdk_manager.instance.dangqian_duihuakuang_shifou_shiyongzhong) {
//             if (this.handle_button_callback) {
//                 this.handle_button_callback();
//             }
//             return void this.node.destroy();
//         }
//         this.click_time++;
//         console.log(this.is_banner_show + "; " + this.isStartShow);
//         if (this.click_time >= this.buttonClickTimes && !this.is_banner_show && !this.isStartShow) {
//             this.isStartShow = !0;
//             var e = this;
//             ailin_sdk_manager.xianshiBannerGuanggao({
//                 successCallback: function () {
//                     if (e) {
//                         e.is_banner_show = !0;
//                     }
//                     if (e) {
//                         e.resumeButton.runAction(cc.moveTo(0.1, new cc.Vec2(e.button_end_position.x, e.button_end_position.y)));
//                     }
//                 },
//                 errorCallback: function () {
//                     e.isStartShow = !1;
//                     e.loadFailTimes++;
//                     if (e.loadFailTimes >= 3 && e) {
//                         e.is_banner_show = !0;
//                     }
//                 }
//             });
//         }
//         if (this.is_banner_show) {
//             if (this.handle_button_callback) {
//                 this.handle_button_callback();
//             }
//             this.node.destroy();
//         }
//     };
//     t.prototype.tianchongYouxiBuju = function () {
//         var e = this;
//         console.log("tianchongYouxiBuju: " + JSON.stringify(ailin_sdk_manager.instance.qg_gengduo_youxi_shuju));
//         var t = ailin_sdk_manager.instance.qg_gengduo_youxi_shuju;
//         this.limitY = Math.ceil(t.length / 3) * o.huoqu_item_jiedian().height + 20 + 50 * (Math.ceil(t.length / 3) - 1) - 600;
//         ailin_sdk_manager.instance.qg_gengduo_youxi_shuju.forEach(function (t) {
//             console.log("forEach: " + JSON.stringify(t));
//             var i = o.huoqu_item_jiedian();
//             if (i) {
//                 cc.loader.load(t.url, function (e, t) {
//                     console.log("--------------: " + JSON.stringify(e) + " ; " + t);
//                     i.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(t);
//                 });
//                 i.on(
//                     cc.Node.EventType.TOUCH_END,
//                     function () {
//                         ailin_sdk_manager.tiaozhuanDaoXinApp({
//                             source: oppo_get_online_dataId,
//                             data: t.jumpData,
//                             success: function () {
//                                 try {
//                                     ailin_sdk_manager.shezhiGuanggaoZhuizong(t.id, 8, t.targetUniqueGame);
//                                 } catch (e) {
//                                     console.log("tiaozhuanDaoXinApp: " + JSON.stringify(e));
//                                 }
//                             }
//                         });
//                     },
//                     i
//                 );
//                 e.node_game_layout.addChild(i);
//                 console.log("====: " + i.getComponent(cc.Sprite).spriteFrame);
//             }
//         });
//     };
//     t.prototype.onDestroy = function () {
//         if (this.xiaohui_huidiao) {
//             this.xiaohui_huidiao();
//         }
//         ailin_sdk_manager.yincangBannerGuanggao();
//     };
//     t.xianshiYouxiZhongjianDuihuaKuang = function (e) {
//         console.log("xianshiYouxiZhongjianDuihuaKuang");
//         var t = o.chuangjian_yuzhiti();
//         t.x = 360;
//         t.y = 640;
//         t.getComponent(o).chushihua_shuju(e);
//         cc.director.getScene().addChild(t);
//     };
//     t.prototype.chushihua_shuju = function (e) {
//         var t = this;
//         if (e.handle_button_callback) {
//             this.handle_button_callback = e.handle_button_callback;
//         }
//         if (e.xiaohui_huidiao) {
//             this.xiaohui_huidiao = e.xiaohui_huidiao;
//         }
//         if (e.button_start_position) {
//             this.button_start_position = e.button_start_position;
//         }
//         if (e.button_end_position) {
//             this.button_end_position = e.button_end_position;
//         }
//         if (e.buttonImgPath) {
//             cc.loader.loadRes(e.buttonImgPath, cc.SpriteFrame, function (e, o) {
//                 e
//                     ? console.log("initDataErr: " + JSON.stringify(e))
//                     : ((t.resumeButton.getComponent(cc.Sprite).spriteFrame = o),
//                       (t.resumeButton.getChildByName("label").active = !1));
//             });
//         }
//         if (e.dianji_cishu) {
//             var o = e.dianji_cishu.min;
//             var i = e.dianji_cishu.max;
//             if (0 == isNaN(o) && 0 == isNaN(i) && o <= i) {
//                 this.buttonClickTimes = Math.floor(Math.random() * (i - o + 1) + o);
//             }
//         }
//     };
//     t.chuangjian_yuzhiti = function () {
//         var e = new cc.Node();
//         e.width = 720;
//         e.height = 1280;
//         var t = e.addComponent(cc.Widget);
//         t.isAlignVerticalCenter = !0;
//         t.isAlignHorizontalCenter = !0;
//         var i = new cc.Node();
//         i.width = 2160;
//         i.height = 3840;
//         i.color = new cc.Color(220, 220, 220, 200);
//         $chestDialog.default.tianjia_jingling_tu(i, "Flower/img/common/whiteBg", p.SIMPLE, g.CUSTOM);
//         i.addComponent(c);
//         e.addChild(i);
//         var n = new cc.Node();
//         n.width = 720;
//         n.height = 1200;
//         $chestDialog.default.tianjia_jingling_tu(n, "Flower/img/common/whiteBg", p.SLICED, g.CUSTOM);
//         var a = n.addComponent(cc.ScrollView);
//         a.vertical = !0;
//         a.inertia = !0;
//         a.brake = 0.75;
//         a.elastic = !0;
//         a.bounceDuration = 0.23;
//         a.cancelInnerEvents = !0;
//         var s = new cc.Node();
//         s.width = 720;
//         s.height = 1200;
//         n.addChild(s);
//         var r = new cc.Node();
//         r.y = 600;
//         r.anchorY = 1;
//         r.width = 700;
//         r.height = 1200;
//         $chestDialog.default.tianjia_jingling_tu(r, "Flower/img/common/whiteBg", p.SLICED, g.CUSTOM);
//         var y = r.addComponent(cc.Layout);
//         y.type = cc.Layout.Type.GRID;
//         y.resizeMode = l.CONTAINER;
//         y.startAxis = d.HORIZONTAL;
//         y.paddingLeft = 30;
//         y.paddingRight = 30;
//         y.paddingTop = 20;
//         y.paddingBottom = 20;
//         y.spacingX = 50;
//         y.spacingY = 50;
//         y.verticalDirection = u.TOP_TO_BOTTOM;
//         y.horizontalDirection = h.LEFT_TO_RIGHT;
//         y.affectedByScale = !1;
//         s.addChild(r);
//         a.content = r;
//         e.addChild(n);
//         var _ = new cc.Node();
//         _.addComponent($buttonAction.default);
//         $chestDialog.default.tianjia_jingling_tu(_, "Flower/img/middleActivity/img/resumeButton", p.SLICED);
//         var v = _.addComponent(cc.Widget);
//         v.isAlignBottom = !0;
//         v.bottom = 40;
//         console.log("-endY: ", _.y);
//         e.addChild(_);
//         var w = e.addComponent(o);
//         w.resumeButton = _;
//         w.node_game_layout = r;
//         console.log("size: ", cc.winSize, cc.view.getFrameSize());
//         var S = cc.winSize.width / 720;
//         var C = cc.winSize.height / 1280;
//         e.scale = S > C ? C : S;
//         return e;
//     };
//     t.huoqu_item_jiedian = function () {
//         var e = new cc.Node();
//         e.width = 180;
//         e.height = 180;
//         $chestDialog.default.tianjia_jingling_tu(e, "Flower/img/common/whiteBg", p.SIMPLE, g.CUSTOM);
//         var t = new cc.Node();
//         t.width = 200;
//         t.height = 200;
//         $chestDialog.default.tianjia_jingling_tu(t, "Flower/img/middleActivity/img/06", p.SIMPLE, g.CUSTOM);
//         e.addChild(t);
//         return e;
//     };
//     __decorate([v(cc.Node)], t.prototype, "node_game_layout", void 0);
//     __decorate([v(cc.Node)], t.prototype, "resumeButton", void 0);
//     return (o = __decorate([_], t));
// })(cc.Component);
// exports.default = w;


/**
 * @Author: jdk
 * @Version: 0.1
 * @Language: 中文
 * @Description: 优化后的代码，提升性能和可读性
 */

const Vec2 = cc.Vec2;
const BlockInputEvents = cc.BlockInputEvents;
const ResizeMode = cc.Layout.ResizeMode;
const AxisDirection = cc.Layout.AxisDirection;
const VerticalDirection = cc.Layout.VerticalDirection;
const HorizontalDirection = cc.Layout.HorizontalDirection;
const SpriteType = cc.Sprite.Type;
const SpriteSizeMode = cc.Sprite.SizeMode;
const chestDialog = require("./AL_Framework_Chest_Dialog");
const buttonAction = require("./AL_Framework_Button_Action");
const { ccclass, property } = cc._decorator;

@ccclass
class ImprovedClass extends cc.Component {
    @property(cc.Node)
    node_game_layout = null;

    @property(cc.Node)
    resumeButton = null;

    button_start_position = new cc.Vec3(0, -540, 0);
    button_end_position = new cc.Vec3(0, -360, 0);
    buttonClickTimes = 2;
    handle_button_callback = null;
    xiaohui_huidiao = null;
    limitY = 0;
    click_time = 0;
    is_banner_show = false;
    isStartShow = false;
    loadFailTimes = 0;

    /**
     * onLoad 生命周期方法
     */
    onLoad() {
        ailin_sdk_manager.yincangBannerGuanggao();
    }

    /**
     * start 生命周期方法
     */
    start() {
        this.resumeButton.position = this.button_start_position;
        this.resumeButton.on(cc.Node.EventType.TOUCH_END, this.handle_event_click_button_resume, this);
        this.tianchongYouxiBuju();

        const moveUpAction = cc.repeatForever(
            cc.sequence(
                cc.moveBy(1, new Vec2(0, 100)),
                cc.callFunc(() => {
                    if (this.node_game_layout.position.y > this.limitY) {
                        console.log("move down");
                        this.node_game_layout.stopAllActions();
                        this.node_game_layout.runAction(moveDownAction);
                    }
                })
            )
        );

        const moveDownAction = cc.repeatForever(
            cc.sequence(
                cc.moveBy(1, new Vec2(0, -100)),
                cc.callFunc(() => {
                    if (this.node_game_layout.position.y < 600) {
                        this.node_game_layout.stopAllActions();
                        this.node_game_layout.runAction(moveUpAction);
                    }
                })
            )
        );

        this.node_game_layout.runAction(moveUpAction);

        const moreGameData = ailin_sdk_manager.instance.qg_gengduo_youxi_shuju[0];
        if (moreGameData) {
            ailin_sdk_manager.tiaozhuanDaoXinApp({
                source: oppo_get_online_dataId,
                data: moreGameData.jumpData,
                success: () => {
                    try {
                        ailin_sdk_manager.shezhiGuanggaoZhuizong(
                            moreGameData.id,
                            8,
                            moreGameData.targetUniqueGame
                        );
                    } catch (error) {
                        console.log("tiaozhuanDaoXinApp: " + JSON.stringify(error));
                    }
                }
            });
        }
    }

    /**
     * 处理恢复按钮点击事件
     */
    handle_event_click_button_resume() {
        console.log("chestDialog handle_event_click_button: 是否走流程 " + ailin_sdk_manager.instance.dangqian_duihuakuang_shifou_shiyongzhong);
        if (!ailin_sdk_manager.instance.dangqian_duihuakuang_shifou_shiyongzhong) {
            if (this.handle_button_callback) {
                this.handle_button_callback();
            }
            this.node.destroy();
            return;
        }

        this.click_time++;
        console.log(this.is_banner_show + "; " + this.isStartShow);
        if (this.click_time >= this.buttonClickTimes && !this.is_banner_show && !this.isStartShow) {
            this.isStartShow = true;
            ailin_sdk_manager.xianshiBannerGuanggao({
                successCallback: () => {
                    this.is_banner_show = true;
                    this.resumeButton.runAction(cc.moveTo(0.1, new Vec2(this.button_end_position.x, this.button_end_position.y)));
                },
                errorCallback: () => {
                    this.isStartShow = false;
                    this.loadFailTimes++;
                    if (this.loadFailTimes >= 3) {
                        this.is_banner_show = true;
                    }
                }
            });
        }

        if (this.is_banner_show) {
            if (this.handle_button_callback) {
                this.handle_button_callback();
            }
            this.node.destroy();
        }
    }

    /**
     * 填充游戏布局
     */
    tianchongYouxiBuju() {
        console.log("tianchongYouxiBuju: " + JSON.stringify(ailin_sdk_manager.instance.qg_gengduo_youxi_shuju));
        const moreGameData = ailin_sdk_manager.instance.qg_gengduo_youxi_shuju;
        this.limitY = Math.ceil(moreGameData.length / 3) * ImprovedClass.huoqu_item_jiedian().height + 20 + 50 * (Math.ceil(moreGameData.length / 3) - 1) - 600;
        
        moreGameData.forEach(data => {
            console.log("forEach: " + JSON.stringify(data));
            const itemNode = ImprovedClass.huoqu_item_jiedian();
            if (itemNode) {
                cc.loader.load(data.url, (error, texture) => {
                    console.log("--------------: " + JSON.stringify(error) + " ; " + texture);
                    itemNode.getComponent(cc.Sprite).spriteFrame = new cc.SpriteFrame(texture);
                });
                itemNode.on(
                    cc.Node.EventType.TOUCH_END,
                    () => {
                        ailin_sdk_manager.tiaozhuanDaoXinApp({
                            source: oppo_get_online_dataId,
                            data: data.jumpData,
                            success: () => {
                                try {
                                    ailin_sdk_manager.shezhiGuanggaoZhuizong(data.id, 8, data.targetUniqueGame);
                                } catch (error) {
                                    console.log("tiaozhuanDaoXinApp: " + JSON.stringify(error));
                                }
                            }
                        });
                    },
                    itemNode
                );
                this.node_game_layout.addChild(itemNode);
                console.log("====: " + itemNode.getComponent(cc.Sprite).spriteFrame);
            }
        });
    }

    /**
     * onDestroy 生命周期方法
     */
    onDestroy() {
        if (this.xiaohui_huidiao) {
            this.xiaohui_huidiao();
        }
        ailin_sdk_manager.yincangBannerGuanggao();
    }

    /**
     * 显示游戏中间对话框
     * @param {Object} data 对话框数据
     */
    static xianshiYouxiZhongjianDuihuaKuang(data) {
        console.log("xianshiYouxiZhongjianDuihuaKuang");
        const dialogNode = ImprovedClass.chuangjian_yuzhiti();
        dialogNode.x = 360;
        dialogNode.y = 640;
        dialogNode.getComponent(ImprovedClass).chushihua_shuju(data);
        cc.director.getScene().addChild(dialogNode);
    }

    /**
     * 初始化数据
     * @param {Object} data 初始化数据
     */
    chushihua_shuju(data) {
        if (data.handle_button_callback) {
            this.handle_button_callback = data.handle_button_callback;
        }
        if (data.xiaohui_huidiao) {
            this.xiaohui_huidiao = data.xiaohui_huidiao;
        }
        if (data.button_start_position) {
            this.button_start_position = data.button_start_position;
        }
        if (data.button_end_position) {
            this.button_end_position = data.button_end_position;
        }
        if (data.buttonImgPath) {
            cc.loader.loadRes(data.buttonImgPath, cc.SpriteFrame, (error, spriteFrame) => {
                if (error) {
                    console.log("initDataErr: " + JSON.stringify(error));
                } else {
                    this.resumeButton.getComponent(cc.Sprite).spriteFrame = spriteFrame;
                    this.resumeButton.getChildByName("label").active = false;
                }
            });
        }

        if (data.dianji_cishu) {
            const minClickTimes = data.dianji_cishu.min;
            const maxClickTimes = data.dianji_cishu.max;
            if (!isNaN(minClickTimes) && !isNaN(maxClickTimes) && minClickTimes <= maxClickTimes) {
                this.buttonClickTimes = Math.floor(Math.random() * (maxClickTimes - minClickTimes + 1) + minClickTimes);
            }
        }
    }

    /**
     * 创建预制件
     * @returns {cc.Node} 预制节点
     */
    static chuangjian_yuzhiti() {
        const rootNode = new cc.Node();
        rootNode.width = 720;
        rootNode.height = 1280;

        const widgetComponent = rootNode.addComponent(cc.Widget);
        widgetComponent.isAlignVerticalCenter = true;
        widgetComponent.isAlignHorizontalCenter = true;

        const bgNode = new cc.Node();
        bgNode.width = 2160;
        bgNode.height = 3840;
        bgNode.color = new cc.Color(220, 220, 220, 200);
        chestDialog.default.tianjia_jingling_tu(bgNode, "Flower/img/common/whiteBg", SpriteType.SIMPLE, SpriteSizeMode.CUSTOM);
        bgNode.addComponent(BlockInputEvents);
        rootNode.addChild(bgNode);

        const scrollViewNode = new cc.Node();
        scrollViewNode.width = 720;
        scrollViewNode.height = 1200;
        chestDialog.default.tianjia_jingling_tu(scrollViewNode, "Flower/img/common/whiteBg", SpriteType.SLICED, SpriteSizeMode.CUSTOM);

        const scrollViewComponent = scrollViewNode.addComponent(cc.ScrollView);
        scrollViewComponent.vertical = true;
        scrollViewComponent.inertia = true;
        scrollViewComponent.brake = 0.75;
        scrollViewComponent.elastic = true;
        scrollViewComponent.bounceDuration = 0.23;
        scrollViewComponent.cancelInnerEvents = true;

        const contentNode = new cc.Node();
        contentNode.width = 720;
        contentNode.height = 1200;
        scrollViewNode.addChild(contentNode);

        const layoutNode = new cc.Node();
        layoutNode.y = 600;
        layoutNode.anchorY = 1;
        layoutNode.width = 700;
        layoutNode.height = 1200;
        chestDialog.default.tianjia_jingling_tu(layoutNode, "Flower/img/common/whiteBg", SpriteType.SLICED, SpriteSizeMode.CUSTOM);

        const layoutComponent = layoutNode.addComponent(cc.Layout);
        layoutComponent.type = cc.Layout.Type.GRID;
        layoutComponent.resizeMode = ResizeMode.CONTAINER;
        layoutComponent.startAxis = AxisDirection.HORIZONTAL;
        layoutComponent.paddingLeft = 30;
        layoutComponent.paddingRight = 30;
        layoutComponent.paddingTop = 20;
        layoutComponent.paddingBottom = 20;
        layoutComponent.spacingX = 50;
        layoutComponent.spacingY = 50;
        layoutComponent.verticalDirection = VerticalDirection.TOP_TO_BOTTOM;
        layoutComponent.horizontalDirection = HorizontalDirection.LEFT_TO_RIGHT;
        layoutComponent.affectedByScale = false;

        contentNode.addChild(layoutNode);
        scrollViewComponent.content = layoutNode;
        rootNode.addChild(scrollViewNode);

        const buttonNode = new cc.Node();
        buttonNode.addComponent(buttonAction.default);
        chestDialog.default.tianjia_jingling_tu(buttonNode, "Flower/img/middleActivity/img/resumeButton", SpriteType.SLICED);

        const buttonWidget = buttonNode.addComponent(cc.Widget);
        buttonWidget.isAlignBottom = true;
        buttonWidget.bottom = 40;

        console.log("-endY: ", buttonNode.y);
        rootNode.addChild(buttonNode);

        const componentInstance = rootNode.addComponent(ImprovedClass);
        componentInstance.resumeButton = buttonNode;
        componentInstance.node_game_layout = layoutNode;

        console.log("size: ", cc.winSize, cc.view.getFrameSize());

        const scaleX = cc.winSize.width / 720;
        const scaleY = cc.winSize.height / 1280;
        rootNode.scale = scaleX > scaleY ? scaleY : scaleX;

        return rootNode;
    }

    /**
     * 获取项目节点
     * @returns {cc.Node} 项目节点
     */
    static huoqu_item_jiedian() {
        const itemNode = new cc.Node();
        itemNode.width = 180;
        itemNode.height = 180;
        chestDialog.default.tianjia_jingling_tu(itemNode, "Flower/img/common/whiteBg", SpriteType.SIMPLE, SpriteSizeMode.CUSTOM);

        const innerNode = new cc.Node();
        innerNode.width = 200;
        innerNode.height = 200;
        chestDialog.default.tianjia_jingling_tu(innerNode, "Flower/img/middleActivity/img/06", SpriteType.SIMPLE, SpriteSizeMode.CUSTOM);
        itemNode.addChild(innerNode);

        return itemNode;
    }
}

exports.default = ImprovedClass;
